/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 * 
 */
package seedpod.webapp.view.seedpodwidgets;

import seedpod.exceptions.InvalidWidgetException;
import seedpod.model.AVPair;
import seedpod.model.ProtegeValueType;
import seedpod.model.SeedpodModel.RdbCls.FormWidget;
import seedpod.model.SeedpodModel.RdbCls.ViewWidget;
import seedpod.model.rdb.RdbValueType;
import seedpod.webapp.view.htmlwidget.TextInput;

public class Numeric extends LimsWidget {
	public Numeric() {
	// TODO Auto-generated constructor stub
	}

	public Numeric(AVPair avpair) {
		super( avpair );
	}

	@Override
	public FormWidget getFormWidgetType() {
		return FormWidget.NUMERIC;
	}

	@Override
	public ViewWidget getViewWidgetType() {
		return ViewWidget.NUMERIC;
	}

	@Override
	public String render() {
		String view = "";
		String data = getData().toString();

		TextInput numericInput = new TextInput( getAttribute().getName(), data );
		numericInput.setCssClass( "text" );
		numericInput.setMaxLength( 20 );
		numericInput.setSize( 10 );
		view += numericInput.render() + getAttribute().getFormWidgetParam(); // parameter may contain unit
		return view;
	}

	/**
	 * @TODO throw different error messages for the different invalid types
	 */
	@Override
	public void validateWidget() throws InvalidWidgetException {
		RdbValueType dbType = getAttribute().getRdbValueType();
		ProtegeValueType prType = getAttribute().getProtegeValueType();

		// Database & protege types must be an integer, numeral, or float
		boolean isValidWidget = (dbType == RdbValueType.INTEGER || dbType == RdbValueType.NUMERIC) && (prType.equals( ProtegeValueType.Float ) || prType.equals( ProtegeValueType.Integer ));
		if ( !isValidWidget ) throw new InvalidWidgetException( "This widget requires a number input." );
	}
}
